using Microsoft.AspNetCore.Mvc;
using Terra.Core.Common.ApiResponse;
using Terra.Core.Common.Exceptions;

namespace Terra.WebApi.Controllers;

/// <summary>
/// 异常测试控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
[ApiExplorerSettings(GroupName = "v1")]
public class TestExceptionController : ControllerBase
{
    /// <summary>
    /// 测试业务异常
    /// </summary>
    /// <remarks>
    /// 抛出一个业务异常，将被全局异常处理中间件捕获并转换为标准响应格式
    /// </remarks>
    /// <response code="400">返回业务异常信息</response>
    [HttpGet("business-exception")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status400BadRequest)]
    public IActionResult TestBusinessException()
    {
        throw new BusinessException("测试业务异常");
    }

    /// <summary>
    /// 测试验证异常
    /// </summary>
    /// <remarks>
    /// 抛出一个验证异常，包含多个字段的验证错误信息
    /// </remarks>
    /// <response code="422">返回验证错误信息</response>
    [HttpGet("validation-exception")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status422UnprocessableEntity)]
    public IActionResult TestValidationException()
    {
        var errors = new Dictionary<string, string[]>
        {
            { "username", new[] { "用户名不能为空" } },
            { "password", new[] { "密码长度必须大于6位" } }
        };
        throw new ValidationException("数据验证失败", errors);
    }

    /// <summary>
    /// 测试权限异常
    /// </summary>
    /// <remarks>
    /// 抛出一个权限异常，模拟用户没有访问权限的情况
    /// </remarks>
    /// <response code="403">返回权限错误信息</response>
    [HttpGet("permission-exception")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status403Forbidden)]
    public IActionResult TestPermissionException()
    {
        throw new PermissionException("没有访问权限");
    }

    /// <summary>
    /// 测试服务器异常
    /// </summary>
    /// <remarks>
    /// 抛出一个未处理的系统异常
    /// </remarks>
    /// <response code="500">返回服务器错误信息</response>
    [HttpGet("server-error")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status500InternalServerError)]
    public IActionResult TestServerError()
    {
        throw new Exception("测试服务器异常");
    }
}